#compdef restart-java-app

# Zsh completion script for restart-java-app
# 使用方法：
# 1. 将此文件复制到 /usr/share/zsh/site-functions/_restart-java-app 或 ~/.zsh/completions/_restart-java-app
# 2. 确保 ~/.zsh/completions 在 fpath 中：fpath=(~/.zsh/completions $fpath)
# 3. 重新加载zsh或运行: autoload -U compinit && compinit

_restart_java_app() {
    local context state line
    typeset -A opt_args
    
    # 获取当前目录下的JAR文件（去掉.jar后缀）
    local jar_files
    jar_files=(${(f)"$(find . -maxdepth 1 -name "*.jar" -type f 2>/dev/null | sed 's|^\./||' | sed 's/\.jar$//')"})
    
    _arguments -C \
        '1: :->action_or_jar' \
        '*: :->jars' && return 0
    
    case $state in
        action_or_jar)
            local actions=(
                'start:启动应用'
                'stop:停止应用'
                'restart:重启应用'
                'status:查看状态'
                'help:显示帮助'
                '-h:显示帮助'
                '--help:显示帮助'
            )
            
            local jar_completions=()
            for jar in $jar_files; do
                jar_completions+=("$jar:JAR包")
            done
            jar_completions+=('all:所有JAR包')
            
            _describe 'actions' actions
            _describe 'jar files' jar_completions
            ;;
        jars)
            local jar_completions=()
            for jar in $jar_files; do
                jar_completions+=("$jar:JAR包")
            done
            jar_completions+=('all:所有JAR包')
            
            _describe 'jar files' jar_completions
            ;;
    esac
}

_restart_java_app "$@"